package kr.co.shoppingmall.reviewboard;

import java.awt.Component;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


@Controller
@RequestMapping("/reviewboard")
public class ReviewBoardController {
	/* log4j */
	public static void showMessageDialog(Component parentComponent, Object message){}
	protected final Log logger = LogFactory.getLog(getClass());
	
	@Resource
	private ReviewBoardService reviewboardService;
	
	@RequestMapping("/ReviewWriteform.htm")//글쓰기
	public String reviewwriteform(ReviewBoardDTO dto, HttpSession session){
		String id = (String)session.getAttribute("id");
		if(id == null){
			JOptionPane.showMessageDialog(null, "로그인 후 사용해 주세요.");
			return "redirect:/index.htm";
		}
		else{
			return "/reviewboard/ReviewWriteform";
		}
	}
	
	@RequestMapping("/ReviewWrite.htm")//글쓰기
	public String reviewwrite(ReviewBoardDTO dto, HttpSession session){
		String id = (String)session.getAttribute("id");
		dto.setRboard_writer(id);
		reviewboardService.insertReviewBoard(dto);
		return "redirect:/reviewboard/ReviewList.htm";
	}
	
	@RequestMapping("/ReviewList.htm")//리스트
	public String reviewlist(@RequestParam(required = false, defaultValue = "1") int pg, Model model){
		//pg 기본 값 : 1, 필수는 아님.
		int rowSize = 10;
		int start = (pg * rowSize) - (rowSize-1);
		int end = (pg * rowSize);
		HashMap map = new HashMap();
		map.put("start", start);
		map.put("end", end);
		List list = reviewboardService.getReviewBoardList(map);
		int rowCount = reviewboardService.getReviewCount();
		int linkSize = 10;
		int startLink = (pg-1) / linkSize * linkSize + 1;
		int endLink = (pg-1) / linkSize * linkSize + linkSize;
		int totalPage = rowCount / rowSize + (rowCount % rowSize == 0 ? 0 :1);
		if(endLink > totalPage){
			endLink = totalPage;
		}
		int max = rowCount -((pg-1) * rowSize);
		model.addAttribute("pg", pg);
		model.addAttribute("linkSize", linkSize);
		model.addAttribute("startLink", startLink);
		model.addAttribute("endLink", endLink);
		model.addAttribute("totalPage", totalPage);
		model.addAttribute("max", max);
		model.addAttribute("list", list);
		return "/reviewboard/ReviewList";
	}
	
	@RequestMapping("/ReviewRead.htm")//글읽기
	public String reviewread(int num, int pg, Model model) {
		reviewboardService.updateReviewBoardHit(num);
		ReviewBoardDTO dto = reviewboardService.getReviewBoard(num);
		model.addAttribute("b", dto);
		model.addAttribute("pg", pg);
		return "/reviewboard/ReviewRead";
	}
	
	@RequestMapping("/ReviewUpdateform.htm")//글수정
	public String reviewupdateform(int num, int pg, Model model, HttpSession session) {
		String id = (String)session.getAttribute("id");
		if(id == null){
			JOptionPane.showMessageDialog(null, "로그인 후 사용해 주세요.");
			return "redirect:/index.htm";
		}
		else{
			ReviewBoardDTO dto = reviewboardService.getReviewBoard(num);
			model.addAttribute("b", dto);
			model.addAttribute("pg", pg);
			return "/reviewboard/ReviewUpdateform";
		}
	}
	
	@RequestMapping("/ReviewUpdate.htm")//글수정
	public String reviewupdate(ReviewBoardDTO dto, int pg) {
		reviewboardService.updateReviewBoard(dto);
		return "redirect:/reviewboard/ReviewList.htm?pg="+pg;
	}
	
	@RequestMapping("/ReviewDeleteform.htm")//글삭제
	public String reviewdeleteform(HttpSession session) {
		String id = (String)session.getAttribute("id");
		if(id == null){
			JOptionPane.showMessageDialog(null, "로그인 후 사용해 주세요.");
			return "redirect:/index.htm";
		}
		else{
			return "/reviewboard/ReviewDeleteform";
		}
	}
	
	@RequestMapping("/ReviewDelete.htm")//글삭제
	public String reviewdelete(ReviewBoardDTO dto, int pg) {
		reviewboardService.deleteReviewBoard(dto);
		return "redirect:/reviewboard/ReviewList.htm?pg="+pg;
	}
	
	@RequestMapping("/ReviewReplyform.htm")//글답변
	public String reviewreplyform(int num, int pg, Model model, HttpSession session) {
		String id = (String)session.getAttribute("id");
		if(id == null){
			JOptionPane.showMessageDialog(null, "로그인 후 사용해 주세요.");
			return "redirect:/index.htm";
		}
		else{
			ReviewBoardDTO dto = reviewboardService.getReviewBoard(num);
			model.addAttribute("b", dto);
			model.addAttribute("pg", pg);
			return "/reviewboard/ReviewReplyform";
		}
	}
	
	@RequestMapping("/ReviewReply.htm")//글답변
	public String reviewreply(ReviewBoardDTO dto, int pg, HttpSession session) {
		String id = (String)session.getAttribute("id");
		dto.setRboard_writer(id);
		dto.setRboard_sort(dto.getRboard_sort() + 1);
		dto.setRboard_tab(dto.getRboard_tab() + 1);
		reviewboardService.updateReviewReply(dto);
		reviewboardService.insertReviewReply(dto);
		return "redirect:/reviewboard/ReviewList.htm?pg="+pg;
	}
}//end
